home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / imap / ANSI / c-client / flock.c < prev    next >
C/C++ Source or Header  |  1996-10-12  |  3KB  |  87 lines

  1. /*
  2.  * Program:    File lock
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    1 August 1988
  13.  * Last Edited:    12 October 1996
  14.  *
  15.  * Copyright 1996 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made available
  24.  * "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36. #include <ustat.h>
  37.  
  38. /* Emulator for BSD flock() call
  39.  * Accepts: file descriptor
  40.  *        operation bitmask
  41.  * Returns: 0 if successful, -1 if failure
  42.  */
  43.  
  44. int bsd_flock (int fd,int operation)
  45. {
  46.   struct stat sbuf;
  47.   struct ustat usbuf;
  48.   struct flock fl;
  49.   int ret;
  50.   char tmp[MAILTMPLEN];
  51.                 /* lock applies to entire file */
  52.   fl.l_whence = fl.l_start = fl.l_len = 0;
  53.   fl.l_pid = getpid ();        /* shouldn't be necessary */
  54.   switch (operation & ~LOCK_NB){/* translate to fcntl() operation */
  55.   case LOCK_EX:            /* exclusive */
  56.     fl.l_type = F_WRLCK;
  57.     break;
  58.   case LOCK_SH:            /* shared */
  59.     fl.l_type = F_RDLCK;
  60.     break;
  61.   case LOCK_UN:            /* unlock */
  62.     fl.l_type = F_UNLCK;
  63.     break;
  64.   default:            /* default */
  65.     errno = EINVAL;
  66.     return -1;
  67.   }
  68.                 /* ftinode should be -1 if NFS */
  69.   if ((!fstat (fd,&sbuf) && !ustat (sbuf.st_dev,&usbuf) && !++usbuf.f_tinode)||
  70.       mail_parameters (NIL,GET_DISABLEFCNTLLOCK,NIL))
  71.     ret = 0;            /* NFS or locking disabled */
  72.   else while (ret = fcntl (fd,(operation & LOCK_NB) ? F_SETLK : F_SETLKW,&fl))
  73.     if (errno != EINTR) {    /* ignore signals */
  74.                 /* non-blocking lock case? */
  75.       if ((operation & LOCK_NB) &&
  76.       ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EACCES))) {
  77.     errno = EWOULDBLOCK;    /* canonicalize error */
  78.     return -1;        /* OK to fail */
  79.       }
  80.       sprintf (tmp,"Unexpected locking failure %s",strerror (errno));
  81.       mm_log (tmp,WARN);    /* give the user a warning of what happened */
  82.       sleep (5);        /* barf */
  83.       break;
  84.     }
  85.   return ret;
  86. }
  87.